package com.tuwan.sensorcatcher;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Author: czmc  Email: @tuwan21.com
 * Version:	1.0  Date:2017/4/7
 */
public class LogFileUtils {
    private static String LOG_PATH ="/sdcard/";// 日志文件在sdcard中的路径
    private static String LOG_FILE = "Log.txt";// 本类输出的日志文件名称
    private static SimpleDateFormat LOGSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 日志的输出格式
    private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
    private static boolean enable = true;
    /**
     * 打开日志文件并写入日志
     *
     * @return
     * **/
    public static void log(String logPath,String fileName,String text) {// 新建或打开日志文件
        if(!enable){
            return;
        }
        LOG_FILE = fileName;
        Date nowtime = new Date();
        String needWriteMessage = text;
        File file = new File(LOG_PATH+logPath, LOG_FILE);
        try {
            FileWriter filerWriter = new FileWriter(file, true);//后面这个参数代表是不是要接上文件中原来的数据，不进行覆盖
            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
            bufWriter.write(needWriteMessage);
//            bufWriter.newLine();
            bufWriter.close();
            filerWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void setLogFold(String fold){
        LOG_PATH = fold;
    }

    public static boolean isEnable() {
        return enable;
    }

    public static void setEnable(boolean enable) {
        LogFileUtils.enable = enable;
    }
}
